package com.easy.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Goods;
import com.easy.common.service.IGoodsService;
import com.easy.common.util.SYS;
import com.easy.goods.mapper.GoodsMapper;
//import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    // 根据id查询商品
    @Override
    public Goods getGoodsById(int id) {
        return super.getOne(new QueryWrapper<Goods>().eq("id", id));
    }
    // 根据名称查询商品
    @Override
    public Goods getGoodsByName(String name) {
        return super.getOne(new QueryWrapper<Goods>().eq("name", name));
    }
    
    @Override
    public int addGoods(Goods goods) {
        // 确保商品状态默认为1(正常)
        if (goods.getStatus() == null) {
            goods.setStatus(1);
        }
        // 确保删除状态默认为0(未删除)
        if (goods.getIs_del() == null) {
            goods.setIs_del(0);
        }
        return super.save(goods) ? 1 : 0;
    }
    
    @Override
    public int updateGoods(Goods goods) {
        goods.setUpdate_time(LocalDateTime.now());
        return super.updateById(goods) ? 1 : 0;
    }

    @Override
    public int deleteGoods(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public int reverseStatus(Integer id) {
        Goods goods = super.getById(id);
        if (goods != null) {
            goods.setStatus(goods.getStatus() == 1 ? 0 : 1);
        }
        return super.updateById(goods) ? 1 : 0;
    }

    @Override
    public List<Goods> getAll(Goods goods) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName, goods.getName());
        queryWrapper.eq(Goods::getStatus, 1);
        queryWrapper.eq(Goods::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Goods> getPage(Goods goods, IPage page) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(goods.getName() != null && !goods.getName().isEmpty(), Goods::getName, goods.getName());
        queryWrapper.like(goods.getCategory() != null && !goods.getCategory().isEmpty(), Goods::getCategory, goods.getCategory());

        queryWrapper.eq(Goods::getIs_del, 0);
        return super.page(page, queryWrapper);
    }

    @Override
    public List<Goods> getGoodsByCategory(String category) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getCategory, category);
        queryWrapper.eq(Goods::getStatus, 1);// 只查询上架的商品
        queryWrapper.eq(Goods::getIs_del, 0); // 只查询未删除的商品
        return super.list(queryWrapper);
    }
}
